package com.ruoyi.cms.controller.app;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.cms.domain.ShortVideo;
import com.ruoyi.cms.service.IShortVideoService;

/**
 * App端短视频Controller
 *
 * @author ruoyi
 * @date 2025-01-05
 */
@RestController
@RequestMapping("/app/shorts")
public class ShortAppController extends BaseController
{
    @Autowired
    private IShortVideoService shortVideoService;

    /**
     * 获取短视频列表（分页）
     */
    @GetMapping("/list")
    public TableDataInfo list(ShortVideo shortVideo)
    {
        startPage();
        // 只返回公开且审核通过的短视频
        shortVideo.setIsPublic("1");
        shortVideo.setStatus("1");
        List<ShortVideo> list = shortVideoService.selectShortVideoList(shortVideo);
        return getDataTable(list);
    }

    /**
     * 获取短视频详情
     */
    @GetMapping("/{shortId}")
    public AjaxResult getShort(@PathVariable Long shortId)
    {
        ShortVideo shortVideo = shortVideoService.selectShortVideoById(shortId);
        if (shortVideo != null && "1".equals(shortVideo.getIsPublic()) && "1".equals(shortVideo.getStatus())) {
            return success(shortVideo);
        }
        return error("短视频不存在或未公开");
    }

    /**
     * 增加短视频播放量
     */
    @PostMapping("/{shortId}/play")
    public AjaxResult increasePlayCount(@PathVariable Long shortId)
    {
        shortVideoService.increaseViewCount(shortId);
        return success();
    }

    /**
     * 获取推荐短视频列表（热门）
     */
    @GetMapping("/hot")
    public TableDataInfo getHotShorts(ShortVideo shortVideo)
    {
        startPage();
        shortVideo.setIsPublic("1");
        shortVideo.setStatus("1");
        // 使用selectHotVideos查询热门视频，limit通过分页参数控制
        List<ShortVideo> list = shortVideoService.selectHotVideos(null);
        return getDataTable(list);
    }

    /**
     * 根据用户ID获取短视频列表
     */
    @GetMapping("/user/{memberUserId}")
    public TableDataInfo getUserShorts(@PathVariable Long memberUserId)
    {
        startPage();
        ShortVideo query = new ShortVideo();
        query.setUserId(memberUserId);
        query.setIsPublic("1");
        query.setStatus("1");
        List<ShortVideo> list = shortVideoService.selectShortVideoList(query);
        return getDataTable(list);
    }
}
